<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

</head>
<body>

<table border="1" width="100%">
  <thead>员工列表
  <tr>
    <td></td>
    <td>员工ID</td>
    <td>员工姓名</td>
    <td>员工性别</td>
    <td>员工生日</td>
    <td>入职日期</td>
    <td>工资</td>
    <td>住址</td>
    <td>部门信息</td>
  </tr>
  </thead>
    <input id="deleteSelect" type="button" value="批量删除" onclick="deleteSelect()"> <input id="editSelect" type="button" value="批量修改">
  <tbody>
    <tr>
      <td>加载中</td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>


  <tfoot>

  </tfoot>
</table>
<a href="emp_add.html">添加</a>
</body>
<script>
  $(document).ready(function () {
    getlist(pageNow);
  })





    function delemp(empId) {
      $.ajax({
        type: "get",
        dataType: "json",
        url: "/admin/emp/dodeljson/"+empId,
        success: function (result) {
          if (result.data > 0){
            console.log("删除成功");
           getlist(pageNow);
           // window.location.reload(true);
          }else {
            alert("删除失败")
          }
        }
      })
    };

    var pageNow=1;
    var pageSize=5;
    var pageCount=1;
    function getlist(pageNow2) {
      $.ajax({
        type: "get",
        dataType: "json",
        url: "/admin/emp/getempsbypage/"+pageNow2 +"/"+ pageSize,
        success: function (result) {
          pageCount = result.data.pages;
          pageNow = result.data.pageNum;
          $("tbody").html("");
          var list = result.data.list;
          for (i = 0; i < list.length; i++) {
            var emp = list[i];
            $("tbody").append(`
              <tr>
                <td><input name="select" type="checkbox" value="${emp.empId}"></td>
                <td>${emp.empId}</td>
                <td>${emp.empName}</td>
                <td>${emp.gender}</td>
                <td>${emp.birthday}</td>
                <td>${emp.hireDate}</td>
                <td>${emp.salary}</td>
                <td>${emp.address}</td>
                <td>${emp.department.deptName}</td>
                <td><a href="emp_edit.html?empId=${emp.empId}"+>修改</a> <a href="javascript:delemp(${emp.empId})" onclick="return confirm('是否删除')">删除</a> </td>
              </tr>
          `)
          }
          getByPages();
        }
      });
    }

    function getByPages() {
      $("tfoot").html("");
      if (pageNow <=1){
        $("tfoot").append("首页   上一页");
      }else {
        $("tfoot").append(`
        <a href="javascript:getlist(1)">首页</a>
        <a href="javascript:getlist(${pageNow-1})">上一页</a>
        `)
      }

      if (pageNow >= pageCount){
        $("tfoot").append("下一页  尾页");
      }else {
        $("tfoot").append(`
        <a href="javascript:getlist(${pageNow+1})">下一页</a>
        <a href="javascript:getlist(${pageCount})">尾页</a>
        `)
      }


    }

    function deleteSelect() {
      var checkNum = $("input[name='select']:checked").length;
      if (checkNum <= 0){
        alert("至少选一个删除");
        return;
      }

      var checkList = new Array()
              $("input[name='select']:checked").each(function () {
        checkList.push($(this).val())
      })
      alert(JSON.stringify(checkList))
      $.ajax({
        type: "post",
        dataType: "json",
        url: "/admin/emp/todellist",
        data: JSON.stringify(checkList),
        contentType:"application/json",
        success:function (result) {
          if (result.errorCode == 500){
            alert(result.errorMsg)
          }else {
            alert("删除成功")
            getlist(pageNow)
          }
        }
      });

    }


</script>
</html>